using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ContainerItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		public enum Anim
		{
			Static = 0,
			Open = 1,
			Close = 2
		}

		private Transform open;

		private Transform close;

		private FrameAnimation frameAnim;

		protected State state;

		protected Anim anim;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			state = (State)data.state;
			frameAnim = frontMask.GetComponent<FrameAnimation>();
			open = model.Find("Open");
			close = model.Find("Close");
		}

		public override void ResetState()
		{
			base.ResetState();
			SetState(state);
		}

		protected override void LateUpdate()
		{
			if (anim != 0 && !frameAnim.isPlaying)
			{
				if (anim == Anim.Open)
				{
					anim = Anim.Static;
					SetState(State.Open);
				}
				else if (anim == Anim.Close)
				{
					anim = Anim.Static;
					SetState(State.Close);
				}
			}
		}

		protected void SetState(State val)
		{
			state = val;
			if (state == State.Open)
			{
				SetOpenColl();
				if (frameAnim == null)
				{
					if (frontMask != null)
					{
						frontMask.gameObject.SetActive(false);
					}
				}
				else
				{
					frameAnim.GotoLastFrame();
				}
				SetPlatformColliderEnable(true);
				SetChildsColliderEnabled(true);
				return;
			}
			SetCloseColl();
			if (frameAnim == null)
			{
				if (frontMask != null)
				{
					frontMask.gameObject.SetActive(true);
				}
			}
			else
			{
				frameAnim.GotoFirstFrame();
			}
			SetPlatformColliderEnable(false);
			SetChildsColliderEnabled(false);
		}

		protected virtual void SetOpenColl()
		{
			BoxCollider2D component = open.GetComponent<BoxCollider2D>();
			coll.offset = component.offset;
			coll.size = component.size;
		}

		protected virtual void SetCloseColl()
		{
			BoxCollider2D component = close.GetComponent<BoxCollider2D>();
			coll.offset = component.offset;
			coll.size = component.size;
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (state == State.Close)
			{
				item.SetAllCollidersEnabled(false);
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}

		public void PlayOCSound(bool isOpen)
		{
			if (base.itemData.clickSound != null && base.itemData.clickSound.Length >= 2)
			{
				if (isOpen)
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.clickSound[0]);
				}
				else
				{
					Singleton<AudioManager>.Instance.PlaySound(base.itemData.clickSound[1]);
				}
			}
		}

		public override void PlayClickSound()
		{
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim != 0)
			{
				return;
			}
			if (state == State.Close)
			{
				if (frameAnim != null)
				{
					anim = Anim.Open;
					frameAnim.Play();
				}
				else
				{
					SetState(State.Open);
				}
				PlayOCSound(true);
			}
			else
			{
				if (frameAnim != null)
				{
					anim = Anim.Close;
					frameAnim.PlayReversed();
				}
				else
				{
					SetState(State.Close);
				}
				PlayOCSound(false);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
